/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.xeerkat.xmpp.server.www;

import java.net.URI;
import org.infoset.xml.Name;
import org.restlet.Application;
import org.restlet.Client;
import org.restlet.Context;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.Restlet;
import org.restlet.data.ChallengeResponse;
import org.restlet.data.ChallengeScheme;
import org.restlet.data.Method;
import org.restlet.data.Reference;
import org.restlet.data.Status;
import org.restlet.routing.Router;
import org.restlet.routing.Template;
import org.restlet.security.Guard;
import org.xeerkat.xmpp.server.XMPPServer;

/**
 *
 * @author alex
 */
public class AdminApplication extends Application {

   static final URI NAMESPACE = URI.create("http://www.xeerkat.org/Vocabulary/Server/2008/1/0");
   static final Name USERS = Name.create(NAMESPACE,"users");
   static final Name USER = Name.create(NAMESPACE,"user");
   static final Name RESOURCES = Name.create(NAMESPACE,"resources");
   static final Name RESOURCE = Name.create(NAMESPACE,"resource");
   static final Name ROSTER = Name.create(NAMESPACE,"roster");
   static final Name CONTACT = Name.create(NAMESPACE,"contact");
   static final Name GROUP = Name.create(NAMESPACE,"group");
   
   XMPPServer server;
   URI authService;
   Reference authRef;
   public AdminApplication(Context context,URI authService,XMPPServer server) {
      super(context);
      this.server = server;
      this.authService = authService;
      this.authRef = new Reference(authService.toString()+"?session=false");
      getContext().getAttributes().put("xmpp.server", server);
   }
   
   public Restlet createRoot() {
      Router router = new Router(getContext());
      Guard guard = new Guard(getContext(),ChallengeScheme.HTTP_BASIC,"Administrators") {
         public boolean checkSecret(String username,char [] secret) {
            String password = new String(secret);
            Request request = new Request(Method.GET,authRef);
            request.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_BASIC,username,password));
            Client client = new Client(authRef.getSchemeProtocol());
            //client.getContext().getAttributes().put("org.restlet.ssl.hostnameVerifier",org.apache.commons.ssl.HostnameVerifier.DEFAULT);
            Response response = client.handle(request);
            return response.getStatus().isSuccess();
         }
      };
      guard.setNext(router);
      router.attach("/u/",AllUsersResource.class).getTemplate().setMatchingMode(Template.MODE_EQUALS);
      router.attach("/r/",ResourcesResource.class).getTemplate().setMatchingMode(Template.MODE_EQUALS);
      router.attach("/r/{id}/kick",KickResourceResource.class).getTemplate().setMatchingMode(Template.MODE_EQUALS);
      router.attach("/u/{id}/roster",UserRosterResource.class).getTemplate().setMatchingMode(Template.MODE_EQUALS);
      router.attach("/u/{id}/",UserResource.class).getTemplate().setMatchingMode(Template.MODE_EQUALS);
      router.attachDefault(new Restlet(getContext()) {
         public void handle(Request request,Response response) {
            response.setStatus(Status.CLIENT_ERROR_NOT_FOUND);
         }
      });
      return guard;
   }
}
